const { HotelModel, CityModel, HotelReservationModel } = require('../../models/index')

// 获取酒店数据
const getHotelData = async (req, res) => {
  try {
    let { filter_type, sort, grade, is_hot, city_id } = req.query

    // 从分页中间件中获取分页信息
    const { offset, pageSize } = req.pagination;

    // 初始化查询对象
    let query = {}
    // 初始化排序数组
    let order = [['hotel_id', 'ASC']]

    // 检查并且构建查询条件
    if (filter_type) {
      query.type = filter_type
    }

    if (grade) {
      query.grade = grade
    }

    if (is_hot) {
      query.is_hot = Number(is_hot)
    }

    if (city_id) {
      query.city_id = city_id
    }

    // 检查并且构建排序条件
    if (sort) {
      switch (sort) {
        case 'comprehensive':
          order = [['updatedAt', 'DESC'], ['createdAt', 'DESC']];
          break;
        case 'price_low_high':
          order = [['price', 'ASC']];
          break;
        case 'price_high_low':
          order = [['price', 'DESC']];
          break;
        case 'rating_high_low':
          order = [['rating', 'DESC']];
          break;
        case 'rating_low_high':
          order = [['rating', 'ASC']];
          break;
      }
    }

    // 根据查询对象查询数据库
    const hotelData = await HotelModel.findAndCountAll({
      where: query,
      order: order,
      attributes: ['hotel_id', 'name', 'price', 'images', 'rating', 'grade', 'type'],
      include: [
        {
          model: CityModel,
          as: 'hotel_locatedCity',
          attributes: ['name']
        }
      ],
      offset: offset,
      limit: pageSize
    })

    // 将数据转化为前端需要的格式
    const formData = hotelData.rows.map(item => {
      return {
        id: item.hotel_id,
        name: item.name,
        price: item.price,
        image: item.images[0].url,
        rating: item.rating,
        grade: item.grade,
        city_name: item.hotel_locatedCity.name,
        type: item.type
      }
    })

    if (hotelData.length === 0) {
      return res.status(200).json({
        title: '酒店数据',
        data: [],
        message: '没有找到符合要求的酒店数据'
      })
    } else {
      res.status(200).json({
        title: '酒店数据',
        data: formData,
        count: hotelData.count
      })
    }

  } catch (error) {
    res.status(500).json({
      error: error.message
    })
  }
}

// 获取热门酒店数据
const getHotHotelData = async (req, res) => {
  try {
    const hotHotel = await HotelModel.findAll({
      where: {
        is_hot: 1
      },
      attributes: ['hotel_id', 'name', 'images'],
      limit: 5
    })

    if (hotHotel.length === 0) {
      return res.status(404).json({
        error: '未找到热门酒店'
      })
    }

    // 返回前端需要的数据格式
    const formData = hotHotel.map(item => {
      return {
        id: item.hotel_id,
        name: item.name,
        image: item.images[0].url
      }
    })

    res.status(200).json({
      title: '热门酒店',
      data: formData
    })
  } catch (error) {
    res.status(500).json({
      error: error.message
    })
  }
}

// 获取酒店详情数据
const getHotelDetailData = async (req, res) => {
  try {
    // 获取酒店ID
    const hotelID = req.params.id

    // 酒店ID为空的情况
    if (!hotelID) {
      return res.status(400).json({
        error: '酒店ID不能为空'
      })
    }

    // 获取酒店详情数据
    const hotelDetail = await HotelModel.findAll({
      where: {
        hotel_id: hotelID,
      },
      include: [
        {
          model: HotelReservationModel,
          as: 'hotel_reservation',
          attributes: ['room_type', 'room_price', 'description']
        }
      ]
    })

    // 未查询到酒店信息的情况
    if (!hotelDetail.length) {
      return res.status(404).json({
        error: '未找到酒店信息'
      })
    }

    res.status(200).json({
      title: '酒店详情信息',
      data: hotelDetail[0]
    })
  } catch (error) {
    res.status(500).json({
      error: error.message
    })
  }
}


module.exports = {
  getHotelData,
  getHotHotelData,
  getHotelDetailData
}